//
//  CXYMyCenterViewController.swift
//  诚叙缘
//
//  Created by sujeking on 17/3/20.
//  Copyright © 2017年 szw. All rights reserved.
//

import UIKit

class CXYMyCenterViewController: CXYBaseTableViewController {
    
    //懒加载的新写法［oc中的getter 方式初始化］
    lazy var info :NSArray = {[]}()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.info = ["1","2","1","2","2","1","2","2","1","2"]
        self.checkUserLogin()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.setupUI()
        self.tableView.reloadData()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func setupUI() {
        self.title = "我的"
        
        let headView :CXYMyInfoHeadView = CXYMyInfoHeadView(frame: CGRect.zero)
        headView.startAllAnimations()
        headView.callBack = {

        }
        self.tableView.tableHeaderView = headView
        self.rightBarButtonItemTitle = "编辑"
        self.rightItemClickAction = #selector(editItenButtonClick)
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (self.info.count)
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "my")
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "my")
            cell?.contentView.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
        }
        cell?.textLabel?.text = "123"
        return cell!;
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row < 5 {
            cell.startAnimationWithDelay(delayTime:(CGFloat)(indexPath.row) / 10 + 0.04)
        }
    }
    
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 84
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
    }
    
    func editItenButtonClick() {
        
    }
}

extension UITableViewCell {
    
    func startAnimationWithDelay(delayTime:CGFloat) {
        self.contentView.transform = CGAffineTransform(translationX:  -UIScreen.main.bounds.width, y: 0)
        UIView.animate(withDuration: 0.2, delay: TimeInterval(delayTime), options: UIViewAnimationOptions.curveLinear, animations: {
            self.contentView.transform = CGAffineTransform.identity
        }, completion: nil)
    }
}

extension CXYMyCenterViewController {
    func checkUserLogin() {
        let loginVC = CXYLoginViewController(nibName: "CXYLoginViewController", bundle: nil)
        self.parent?.present(loginVC, animated: true, completion: nil)
    }
}
